<?php
    // 接受前端发送过来的数据
    $username = $_POST["username"];
    $password = $_POST["password"];
    // 链接数据库
    $conn = mysqli_connect("localhost", "root", "123456", "sbeam");
    // 查询
    $result = mysqli_query($conn, "SELECT * FROM users where username='$username' and password='$password'");
    // 判断有没有查询到内容
    $num = mysqli_num_rows($result);
    //查询出来是二维数组
    $arr = mysqli_fetch_all($result,MYSQLI_ASSOC);

    $time = time()+1000*60*60;
    if ($num === 0) {
        echo json_encode(array("error" => 1, "data" => "用户名或密码错误"));
    } else {
        setcookie("isLogin","1",$time,"/");
        setcookie("username",$username,$time,"/");
        setcookie("nickname",$arr[0]['nickname'],$time,"/");
        setcookie("id",$arr[0]['id'],$time,"/");

        echo json_encode(array("error" => 0, "data" => "登录成功！"));
    }
?>